package com.yanzhiyi.user.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yanzhiyi.user.mapper.RoleMapper;
import com.yanzhiyi.user.mapper.RoleMenuMapper;
import com.yanzhiyi.user.mapper.RolePermissionMapper;
import com.yanzhiyi.user.pojo.Role;
import com.yanzhiyi.user.pojo.RoleMenu;
import com.yanzhiyi.user.pojo.RolePermission;
import com.yanzhiyi.user.pojo.SearchCondition;
import com.yanzhiyi.user.service.RoleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * FileName: RoleServiceImpl
 * Author:   xuan zongjun
 * Date:     2020/10/21 15:51
 * Description:
 */

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    /**
     * 查询所有角色
     *
     * @return
     */
    @Override
    public List<Role> findAll() {
        return roleMapper.selectAll();
    }

    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> findById(Integer id) {
        Map<String, Object> map = new HashMap<>();
        Role role = roleMapper.selectByPrimaryKey( id );
        map.put( "role", role );

        //获取菜单
        Example example = new Example( RoleMenu.class );
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo( "roleId", id );
        List<RoleMenu> menus = roleMenuMapper.selectByExample( example );
        List<Integer> menuIds = new ArrayList<>(  );
        for (RoleMenu menu : menus) {
            menuIds.add( menu.getMenuId() );
        }
        map.put( "menuIds", menuIds );

        //获取权限
        Example example2 = new Example( RolePermission.class );
        Example.Criteria criteria2 = example.createCriteria();
        criteria2.andEqualTo( "roleId", id );
        List<RolePermission> permissions = rolePermissionMapper.selectByExample( example2 );
        List<Integer> permissionIds = new ArrayList<>(  );
        for (RolePermission permission : permissions) {
            permissionIds.add( permission.getPermissionId() );
        }
        map.put( "permissionIds", permissionIds );

        return map;
    }

    /**
     * 添加角色
     *
     * @param role
     */
    @Override
    public void add(Role role, Integer[] menus, Integer[] permissionIds) {
        role.setStatus( 0 );
        role.setIsDelete( 0 );
        role.setCreateTime( new Date() );
        role.setCreateBy( 600001 );
        roleMapper.insertSelective( role );

        //添加角色和菜单
        if (menus != null && menus.length > 0) {
            for (Integer menuId : menus) {
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setMenuId( menuId );
                roleMenu.setRoleId( role.getId() );
                roleMenuMapper.insert( roleMenu );
            }
        }

        //添加角色和权限
        if (permissionIds != null && permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                RolePermission rolePermission = new RolePermission();
                rolePermission.setRoleId( role.getId() );
                rolePermission.setPermissionId( permissionId );
                rolePermissionMapper.insert( rolePermission );


            }
        }


    }

    /**
     * 修改角色
     *
     * @param role
     */
    @Override
    public void update(Role role, Integer[] menus, Integer[] permissionIds) {
        roleMapper.updateByPrimaryKeySelective( role );


        //首先删除角色和菜单
        Example example = new Example( RoleMenu.class );
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo( "roleId",role.getId() );
        roleMenuMapper.deleteByExample( example );
        //添加角色和菜单
        if (menus != null && menus.length > 0) {
            for (Integer menuId : menus) {
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setMenuId( menuId );
                roleMenu.setRoleId( role.getId() );
                roleMenuMapper.insert( roleMenu );
            }
        }

        //首先删除角色和菜单
        Example example2 = new Example( RolePermission.class );
        Example.Criteria criteria2 = example.createCriteria();
        criteria2.andEqualTo( "roleId",role.getId() );
        rolePermissionMapper.deleteByExample( example2 );

        //添加角色和权限
        if (permissionIds != null && permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                RolePermission rolePermission = new RolePermission();
                rolePermission.setRoleId( role.getId() );
                rolePermission.setPermissionId( permissionId );
                rolePermissionMapper.insert( rolePermission );


            }
        }



    }

    /**
     * 删除角色
     *
     * @param id
     */
    @Override
    public void delete(Integer id) {

        roleMapper.deleteByPrimaryKey( id );

    }

    /**
     * 多条件搜索角色方法
     *
     * @param searchMap
     * @return
     */
    @Override
    public List<Role> findList(Map<String, Object> searchMap) {
        Example example = new Example( Role.class );
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            //角色名称
            if (searchMap.get( "name" ) != null && !"".equals( searchMap.get( "name" ) )) {
                criteria.andLike( "name", "%" + searchMap.get( "name" ) + "%" );
            }
            //角色ID
            if (searchMap.get( "id" ) != null && !"".equals( searchMap.get( "id" ) )) {
                criteria.andEqualTo( "id", searchMap.get( "id" ) );
            }


        }
        return roleMapper.selectByExample( example );
    }

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Role> findPage(int page, int size) {
        PageHelper.startPage( page, size );
        return (Page<Role>) roleMapper.selectAll();
    }

    /**
     * 分页查询 和 多条件查询
     *
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Role> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage( page, size );
        Example example = new Example( Role.class );
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            //名称查询
            if (searchMap.get( "name" ) != null && !"".equals( searchMap.get( "name" ) )) {
                criteria.andLike( "name", "%" + searchMap.get( "name" ) + "%" );
            }
            //ID查询
            if (searchMap.get( "id" ) != null && "".equals( searchMap.get( "id" ) )) {
                criteria.andEqualTo( "id", searchMap.get( "id" ) );
            }
        }

        return (Page<Role>) roleMapper.selectByExample( example );
    }

    /**
     * 分页查询 和 多条件查询
     *
     * @param searchCondition
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Role> searchPage(SearchCondition searchCondition, int page, int size) {
        PageHelper.startPage( page, size );
        Example example = new Example( Role.class );
        Example.Criteria criteria = example.createCriteria();
        if (searchCondition != null) {
            //名称查询
            if (!StringUtils.isEmpty( searchCondition.getName() )) {
                criteria.andLike( "name", "%" + searchCondition.getName() + "%" );
            }
            //ID查询
            if (searchCondition.getId() != null) {
                criteria.andEqualTo( "id", searchCondition.getId() );
            }
        }

        return (Page<Role>) roleMapper.selectByExample( example );
    }
}
